# coding: utf-8
import json
import logging
import re
import threading


class JZThread(threading.Thread):
    """
        对JZ_part2中传入的讲座编号进行报名讲座操作
    """
    def __init__(self, username, wid, name, session_request, getjz_url, login_headers):
        threading.Thread.__init__(self)
        self.username = username #学号
        self.wid = wid #讲座编号
        self.name = name #讲座名称
        self.session_request = session_request #session会话
        self.getjz_url = getjz_url #报名讲座的url
        self.login_headers = login_headers #header信息

    def run(self):
        self.QJZ()

    def QJZ(self):
        try:
            logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            logger = logging.getLogger(__name__)
            data_jz = dict(JZXX=self.wid)
            jzhq_result = self.session_request.post(self.getjz_url, data=data_jz, headers=self.login_headers, timeout=5)
            # 进行报名讲座操作
            inf_dict = json.loads(jzhq_result.content.decode('utf-8'))
            meg = inf_dict.get('msg')
            if re.search('成功', meg) is not None:
                logger.info("学号：" + self.username + " 讲座题目：" + self.name + " 结果：" + meg)
            # else:
            #   logger.info("学号：" + self.username + " 讲座题目：" + self.name + " 结果：" + meg)
        except Exception as err:
            logger.warning(err)
